<?php
include_once('../lib/Snoopy.class.php');

//数据处理
class Model extends Db{

    private $error = null;
    private $insert_id = null;
    private $Http = null;
    const KEY = 'fsfg434@wtgetgc3r345';

	function __construct($uri = ''){
		$this->Http = new Snoopy;
        $this->uri = $uri;
	}

    public function getUri($op) {
        $time = time();
        $uri = $this->uri . "&a={$op}&time={$time}&auth=".md5(self::KEY . $time); 

        return $uri;
    } 

    public function find($table,$where,$order = 'id desc',$limit = '0,1'){
        $args = func_get_args();
        $uri = $this->getUri('find');

        $returnData = $this->Http->submit($uri, ['args' => json_encode($args)]);
        $result = json_decode($returnData->results, true);
 
        if($result['status'] == 1) {
            return $result['data'];
        }
        else {
            return [];
        }
    }

    public function add($table, $data = []) {
        $args = func_get_args();
        $uri = $this->getUri('add');

        $this->insert_id = $this->error = null;

        $returnData = $this->Http->submit($uri, ['args' => json_encode($args)]);
        $result = json_decode($returnData->results, true);
 
        if($result['status'] == 1) {
            $this->insert_id = $result['data']['id'];
            return $result['data']['id'];
        }
        else {
            $this->error = $result['info'];
            return false;
        }
    }

    public function save($table, $where = '', $data = []) {
        $args = func_get_args();
        $uri = $this->getUri('save');

        $this->error = null;

        $returnData = $this->Http->submit($uri, ['args' => json_encode($args)]);
        $result = json_decode($returnData->results, true);
        if($result['status'] == 1) {
            return $result['data']['id'];
        }
        else {
            $this->error = $result['info'];
            return false;
        }
    }

    public function query($sql) {
        $uri = $this->getUri('query');

        $returnData = $this->Http->submit($uri, ['sql' => $sql]);
        $result = json_decode($returnData->results, true);
        if($result['status'] == 1) {
            return $result['data'];
        }
        else {
            return false;
        }
    }

    public function insert_id() {
        return $this->insert_id;
    }

    public function error($msg='') {
        return $this->error;
    }
	
	public function checkSalve(){
        global $redis;

        //判断同步状态
        if($this->dbMode == 'slave' && 1==0) {
            $sql = "show slave status";
            $record = array();
            $result = mysql_query($sql, $this->conn);
            while($row = mysql_fetch_assoc($result)){
                $record[] = $row;           
            }
            $result = $record[0];
 
            $slave_IO_Running  = $result['Slave_IO_Running'];
            $slave_SQL_Running = $result['Slave_SQL_Running'];
            if($slave_IO_Running != 'Yes' || $slave_SQL_Running != 'Yes') {
                $in = $redis instanceOf Redis;
                if($in) {
                    $redis->set('LNPAN_MH_QUEUE_STOP', 1);
                    $redis->set('LNPAN_QUEUE_STOP', 1);
                    die('slave error');
                }
            }
        }
    }
}

